<template>
  <filterInput>
    :border="border" :placeholder="placeholder"
    :emptyTips="emptyTips":businessId="fundId" @getlist="getlist"
    @getCandidates="getCandidates" @getInputItem="getItemList"
    @getItemId="getItemId"
  </filterInput>
</template>

<script>
import filterInput from "../filterInput/filterInput.vue";
import { getFunds } from "@/apis/api";
import { get } from "../../apis/request";

export default {
  name: "filter-Input",
  props: {
    border: {
      type: Boolean,
      default: true,
    },
    placeholder: {
      type: String,
      default: "",
    },
    emptyTips: {
      type: String,
      default: " ",
    },
    fundId: {
      type: String,
      default: "",
    },
  },
  // watch: {
  //   // fundId: {
  //   //   handler(newVal) {
  //   //     this.$emit("update:fundId", newVal);
  //   //   },
  //   //   immediate: true,
  //   // },
  // },
  onload() {
    uni.$on("getlist", (rel) => {
      console.log("rel-getlist");
    });
  },
  methods: {
    getlist(keywords, callback) {
      var para = {
        pageSize: 10,
        keywords: keywords,
      };

      var itemList = [];
      getFunds(para)
        .then((res) => {
          res.data.data.forEach((e) => {
            itemList.push({
              fundId: e.fundId,
              name: e.name,
              level: e.level,
              display: `[${e.level}] ${e.name}`,
            });
          });
          callback(itemList);
        })
        .catch((err) => {
          console.log("err", err);
          callback(itemList);
        });
    },
    getCandidates(itemList, callback) {
      var items = [];
      itemList.forEach((e) => {
        newItems.push(`${e.display}`);
      });
      callback(items);
    },

    getItem(itemList, inputVal, callback) {
      let item = itemList.find((x) => x.display == inputVal);
      if (!item) {
        let index = inputVal.trim().indexOf("] ");
        let keywords =
          index > 0 ? inputVal.trim().substring(index) : inputVal.trim();

        item = itemList.find((x) => x.name == keywords);
      }
      callback(item);
    },
    getItemId(item, callback) {
      callback(item ? item.fundId : "");
    },
  },
};
</script>
